package com.xinchuang.controller;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xinchuang.service.actual.TryOutDeviceModService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import com.xinchuang.utils.CheckUtil;
import com.xinchuang.comment.R;
import com.xinchuang.comment.ResultCode;
import com.google.gson.Gson;

import java.text.SimpleDateFormat;

import com.xinchuang.utils.DateUtils;
import org.springframework.web.multipart.MultipartFile;
import org.apache.commons.lang3.StringUtils;

/**
 * TryoutDeviceModController
 *
 * @date 2023-08-04
 */
@RestController
@RequestMapping("/tryoutDeviceMod")
@Api(value = "试模设备模块", tags = "试模设备模块")
public class TryOutDeviceModController {
    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Resource
    private TryOutDeviceModService tryOutDeviceModService;

    /**
     * 获取试模设备列表接口  函数名称getDeviceList  功能描述:
     *
     * @param jsonObject 中文名:通用实体类
     * @return 返回参数{}
     */
    @PostMapping("/getDeviceList")
    @ApiOperation("获取试模设备列表")
    public R getDeviceList(@RequestBody JSONObject jsonObject) {
        String methodName = Thread.currentThread().getStackTrace()[1].getMethodName();
        Set<String> sSet = new HashSet<>();
        String result = jsonLoop(jsonObject, "", methodName + "SC", sSet);
        result = result + getDeviceListACS(sSet);
        if (result.length() > 0) {
            return R.fail("入参：" + result);
        } else {
            // 调用业务层代码
            String r = tryOutDeviceModService.getDeviceList(jsonObject);
            return JSON.parseObject(r, R.class);
        }
    }

    /**
     * 添加试模设备接口接口  函数名称addDevice  功能描述:
     *
     * @param jsonObject 中文名:通用实体类
     * @return 返回参数{}
     */
    @PostMapping("/addDevice")
    @ApiOperation("添加试模设备接口")
    public R addDevice(@RequestBody JSONObject jsonObject) {
        String methodName = Thread.currentThread().getStackTrace()[1].getMethodName();
        Set<String> sSet = new HashSet<>();
        String result = jsonLoop(jsonObject, "", methodName + "SC", sSet);
        result = result + addDeviceACS(sSet);
        if (result.length() > 0) {
            return R.fail("入参：" + result);
        } else {
            // 调用业务层代码
            String r = tryOutDeviceModService.addDevice(jsonObject);
            Set<String> aSet = new HashSet<>();
            JSONObject o = JSON.parseObject(r);
            if (o == null) {
                o = new JSONObject();
            }
            result = jsonLoop(o.get("data"), "", methodName + "AC", aSet);
            result = result + addDeviceACB(aSet);
            if (result.length() > 0) {
                return R.fail("出参：" + result);
            }
            return JSON.parseObject(r, R.class);
        }
    }

    /**
     * 保存试模接口接口  函数名称saveDevice  功能描述:
     *
     * @param jsonObject 中文名:通用实体类
     * @return 返回参数{}
     */
    @PostMapping("/saveDevice")
    @ApiOperation("保存试模接口")
    public R saveDevice(@RequestBody JSONObject jsonObject) {
        String methodName = Thread.currentThread().getStackTrace()[1].getMethodName();
        Set<String> sSet = new HashSet<>();
        String result = jsonLoop(jsonObject, "", methodName + "SC", sSet);
        result = result + saveDeviceACS(sSet);
        if (result.length() > 0) {
            return R.fail("入参：" + result);
        } else {
            // 调用业务层代码
            String r = tryOutDeviceModService.saveDevice(jsonObject);
            Set<String> aSet = new HashSet<>();
            JSONObject o = JSON.parseObject(r);
            if (o == null) {
                o = new JSONObject();
            }
            result = jsonLoop(o.get("data"), "", methodName + "AC", aSet);
            result = result + saveDeviceACB(aSet);
            if (result.length() > 0) {
                return R.fail("出参：" + result);
            }
            return JSON.parseObject(r, R.class);
        }
    }

    /**
     * 删除试模设备接口接口  函数名称deleteDevice  功能描述:
     *
     * @param jsonObject 中文名:通用实体类
     * @return 返回参数{}
     */
    @PostMapping("/deleteDevice")
    @ApiOperation("删除试模设备接口")
    public R deleteDevice(@RequestBody JSONObject jsonObject) {
        String methodName = Thread.currentThread().getStackTrace()[1].getMethodName();
        Set<String> sSet = new HashSet<>();
        String result = jsonLoop(jsonObject, "", methodName + "SC", sSet);
        result = result + deleteDeviceACS(sSet);
        if (result.length() > 0) {
            return R.fail("入参：" + result);
        } else {
            // 调用业务层代码
            String r = tryOutDeviceModService.deleteDevice(jsonObject);
            Set<String> aSet = new HashSet<>();
            JSONObject o = JSON.parseObject(r);
            if (o == null) {
                o = new JSONObject();
            }
            result = jsonLoop(o.get("data"), "", methodName + "AC", aSet);
            result = result + deleteDeviceACB(aSet);
            if (result.length() > 0) {
                return R.fail("出参：" + result);
            }
            return JSON.parseObject(r, R.class);
        }
    }

    /**
     * 试模设备名称查询接口  函数名称queryDeviceByName  功能描述:
     *
     * @param jsonObject 中文名:通用实体类
     * @return 返回参数{}
     */
    @PostMapping("/queryDeviceByName")
    @ApiOperation("试模设备名称查询")
    public R queryDeviceByName(@RequestBody JSONObject jsonObject) {
        String methodName = Thread.currentThread().getStackTrace()[1].getMethodName();
        Set<String> sSet = new HashSet<>();
        String result = jsonLoop(jsonObject, "", methodName + "SC", sSet);
        result = result + queryDeviceByNameACS(sSet);
        if (result.length() > 0) {
            return R.fail("入参：" + result);
        } else {
            // 调用业务层代码
            String r = tryOutDeviceModService.queryDeviceByName(jsonObject);
            return JSON.parseObject(r, R.class);
        }
    }


    public static String jsonLoop(Object object, String result, String methodName, Set<String> set) {
        Object v = null;
        if (object instanceof JSONArray || object instanceof ArrayList) {
            JSONArray jsonArray = new JSONArray();
            if (object instanceof ArrayList) {
                jsonArray = JSONArray.parseArray(JSON.toJSONString(object));
            } else {
                jsonArray = (JSONArray) object;
            }
            for (int i = 0; i < jsonArray.size(); i++) {
                result = jsonLoop(jsonArray.get(i), result, methodName, set);
                if (StringUtils.isNotEmpty(result)) {
                    return result;
                }
            }
        }
        if (object instanceof JSONObject) {
            JSONObject jsonObject = (JSONObject) object;
            for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
                Object o = entry.getValue();
                if (o instanceof JSONArray || o instanceof ArrayList) {
                    result += selectCheckMathod(entry.getKey(), entry.getValue(), methodName, set);
                    result = jsonLoop(o, result, methodName, set);
                    if (StringUtils.isNotEmpty(result)) {
                        return result;
                    }
                } else if (o instanceof JSONObject) {
                    result = jsonLoop(o, result, methodName, set);
                    if (StringUtils.isNotEmpty(result)) {
                        return result;
                    }
                } else {
//                   FIXME: 未知类型
                }
                v = entry.getValue();
                result += selectCheckMathod(entry.getKey(), v, methodName, set);
            }
        }
        return result;
    }

    private static String selectCheckMathod(String key, Object value, String methodName, Set<String> set) {
        String result = "";
        if ("getDeviceListSC".equals(methodName)) {
            result = getDeviceListSC(key, value, set);
        }
        if ("getDeviceListAC".equals(methodName)) {
            result = getDeviceListAC(key, value, set);
        }
        if ("addDeviceSC".equals(methodName)) {
            result = addDeviceSC(key, value, set);
        }
        if ("addDeviceAC".equals(methodName)) {
            result = addDeviceAC(key, value, set);
        }
        if ("saveDeviceSC".equals(methodName)) {
            result = saveDeviceSC(key, value, set);
        }
        if ("saveDeviceAC".equals(methodName)) {
            result = saveDeviceAC(key, value, set);
        }
        if ("deleteDeviceSC".equals(methodName)) {
            result = deleteDeviceSC(key, value, set);
        }
        if ("deleteDeviceAC".equals(methodName)) {
            result = deleteDeviceAC(key, value, set);
        }
        if ("queryDeviceByNameSC".equals(methodName)) {
            result = queryDeviceByNameSC(key, value, set);
        }
        if ("queryDeviceByNameAC".equals(methodName)) {
            result = queryDeviceByNameAC(key, value, set);
        }
        return result;
    }


    private static String getDeviceListSC(String key, Object value, Set<String> set) {
        String result = "";
        Boolean isNull = false;
        if ("serialNumber".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "规格型号", isNull, 0, 255);
        }
        if ("pageNum".equals(key)) {
            isNull = true;
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "页码", isNull, 0, 255);
        }
        if ("pageSize".equals(key)) {
            isNull = true;
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "每页数量", isNull, 0, 255);
        }
        if (isNull) {
            set.add(key);
        }
        return result;
    }

    private static String getDeviceListAC(String key, Object value, Set<String> set) {
        String result = "";
        Boolean isNull = false;
        if ("code".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkBigDecimal(value == null ? "" : value.toString(), "状态码", isNull, "0", "100000");
        }
        if ("msg".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "返回消息", isNull, 0, 255);
        }
        if ("success".equals(key)) {
            // 判断参数类型: String  num boolean datetime
        }
        if ("dataList".equals(key)) {
            result += CheckUtil.checkJSONArray(JSONArray.parseArray(JSON.toJSONString(value)), "数据列表", isNull);
        }
        if ("deviceName".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "试模设备型号", isNull, 0, 255);
        }
        if ("serialNumber".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "规格型号", isNull, 0, 255);
        }
        if ("normalTimePrice".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "标准时间单价", isNull, 0, 255);
        }
        if ("deviceId".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "设备Id", isNull, 0, 255);
        }
        if ("totalNum".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkBigDecimal(value == null ? "" : value.toString(), "总数据量", isNull, "0", "255");
        }
        if ("timeOutPirce".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "超出标准时间单价", isNull, 0, 255);
        }
        if ("price".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "单价", isNull, 0, 255);
        }
        if (isNull) {
            set.add(key);
        }
        return result;
    }

    private static String addDeviceSC(String key, Object value, Set<String> set) {
        String result = "";
        Boolean isNull = false;
        if ("deviceName".equals(key)) {
            isNull = true;
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "试模设备型号", isNull, 0, 255);
        }
        if ("serialNumber".equals(key)) {
            isNull = true;
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "规格型号", isNull, 0, 255);
        }
        if ("normalTimePrice".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "标准时间单价", isNull, 0, 255);
        }
        if ("timeOutPirce".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "超出标准时间单价", isNull, 0, 255);
        }
        if ("price".equals(key)) {
            isNull = true;
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "单价", isNull, 0, 255);
        }
        if (isNull) {
            set.add(key);
        }
        return result;
    }

    private static String addDeviceAC(String key, Object value, Set<String> set) {
        String result = "";
        Boolean isNull = false;
        if ("code".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkBigDecimal(value == null ? "" : value.toString(), "状态码", isNull, "0", "100000");
        }
        if ("msg".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "返回消息", isNull, 0, 255);
        }
        if ("success".equals(key)) {
            // 判断参数类型: String  num boolean datetime
        }
        if (isNull) {
            set.add(key);
        }
        return result;
    }

    private static String saveDeviceSC(String key, Object value, Set<String> set) {
        String result = "";
        Boolean isNull = false;
        if ("deviceName".equals(key)) {
            isNull = true;
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "试模设备型号", isNull, 0, 255);
        }
        if ("serialNumber".equals(key)) {
            isNull = true;
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "规格型号", isNull, 0, 255);
        }
        if ("normalTimePrice".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "标准时间单价", isNull, 0, 255);
        }
        if ("deviceId".equals(key)) {
            isNull = true;
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "设备id", isNull, 0, 255);
        }
        if ("timeOutPirce".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "超出标准时间单价", isNull, 0, 255);
        }
        if ("price".equals(key)) {
            isNull = true;
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "单价", isNull, 0, 255);
        }
        if (isNull) {
            set.add(key);
        }
        return result;
    }

    private static String saveDeviceAC(String key, Object value, Set<String> set) {
        String result = "";
        Boolean isNull = false;
        if ("code".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkBigDecimal(value == null ? "" : value.toString(), "状态码", isNull, "0", "100000");
        }
        if ("msg".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "返回消息", isNull, 0, 255);
        }
        if ("success".equals(key)) {
            // 判断参数类型: String  num boolean datetime
        }
        if (isNull) {
            set.add(key);
        }
        return result;
    }

    private static String deleteDeviceSC(String key, Object value, Set<String> set) {
        String result = "";
        Boolean isNull = false;
        if ("deviceId".equals(key)) {
            isNull = true;
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "设备Id", isNull, 0, 255);
        }
        if (isNull) {
            set.add(key);
        }
        return result;
    }

    private static String deleteDeviceAC(String key, Object value, Set<String> set) {
        String result = "";
        Boolean isNull = false;
        if ("code".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkBigDecimal(value == null ? "" : value.toString(), "状态码", isNull, "0", "100000");
        }
        if ("msg".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "返回消息", isNull, 0, 255);
        }
        if ("success".equals(key)) {
            // 判断参数类型: String  num boolean datetime
        }
        if (isNull) {
            set.add(key);
        }
        return result;
    }

    private static String queryDeviceByNameSC(String key, Object value, Set<String> set) {
        String result = "";
        Boolean isNull = false;
        if ("deviceName".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "试模设备型号", isNull, 0, 255);
        }
        if (isNull) {
            set.add(key);
        }
        return result;
    }

    private static String queryDeviceByNameAC(String key, Object value, Set<String> set) {
        String result = "";
        Boolean isNull = false;
        if ("code".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkBigDecimal(value == null ? "" : value.toString(), "状态码", isNull, "0", "100000");
        }
        if ("msg".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "返回消息", isNull, 0, 255);
        }
        if ("success".equals(key)) {
            // 判断参数类型: String  num boolean datetime
        }
        if ("dataList".equals(key)) {
            result += CheckUtil.checkJSONArray(JSONArray.parseArray(JSON.toJSONString(value)), "数据列表", isNull);
        }
        if ("deviceName".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "试模设备型号", isNull, 0, 255);
        }
        if ("serialNumber".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "规格型号", isNull, 0, 255);
        }
        if ("normalTimePrice".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "标准时间单价", isNull, 0, 255);
        }
        if ("timeOutPirce".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "超出标准时间单价", isNull, 0, 255);
        }
        if ("deviceId".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "设备Id", isNull, 0, 255);
        }
        if ("price".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "单价", isNull, 0, 255);
        }
        if (isNull) {
            set.add(key);
        }
        return result;
    }

    private static String getDeviceListACS(Set<String> set) {
        List<String> list = new ArrayList<>();
        list.add("pageSize");
        list.add("pageNum");
        if (set.size() == list.size()) {
            return "";
        }
        return getCollectionDifferentValue(list, set, "入参");
    }

    private static String getDeviceListACB(Set<String> set) {
        List<String> list = new ArrayList<>();
        if (set.size() == list.size()) {
            return "";
        }
        return getCollectionDifferentValue(list, set, "出参");
    }

    private static String addDeviceACS(Set<String> set) {
        List<String> list = new ArrayList<>();
        list.add("serialNumber");
        list.add("price");
        list.add("deviceName");
        if (set.size() == list.size()) {
            return "";
        }
        return getCollectionDifferentValue(list, set, "入参");
    }

    private static String addDeviceACB(Set<String> set) {
        List<String> list = new ArrayList<>();
        if (set.size() == list.size()) {
            return "";
        }
        return getCollectionDifferentValue(list, set, "出参");
    }

    private static String saveDeviceACS(Set<String> set) {
        List<String> list = new ArrayList<>();
        list.add("serialNumber");
        list.add("price");
        list.add("deviceName");
        list.add("deviceId");
        if (set.size() == list.size()) {
            return "";
        }
        return getCollectionDifferentValue(list, set, "入参");
    }

    private static String saveDeviceACB(Set<String> set) {
        List<String> list = new ArrayList<>();
        if (set.size() == list.size()) {
            return "";
        }
        return getCollectionDifferentValue(list, set, "出参");
    }

    private static String deleteDeviceACS(Set<String> set) {
        List<String> list = new ArrayList<>();
        list.add("deviceId");
        if (set.size() == list.size()) {
            return "";
        }
        return getCollectionDifferentValue(list, set, "入参");
    }

    private static String deleteDeviceACB(Set<String> set) {
        List<String> list = new ArrayList<>();
        if (set.size() == list.size()) {
            return "";
        }
        return getCollectionDifferentValue(list, set, "出参");
    }

    private static String queryDeviceByNameACS(Set<String> set) {
        List<String> list = new ArrayList<>();
        if (set.size() == list.size()) {
            return "";
        }
        return getCollectionDifferentValue(list, set, "入参");
    }

    private static String queryDeviceByNameACB(Set<String> set) {
        List<String> list = new ArrayList<>();
        if (set.size() == list.size()) {
            return "";
        }
        return getCollectionDifferentValue(list, set, "出参");
    }

    /**
     * 获取集合中的不同值
     *
     * @param list
     * @param set
     * @return
     */
    private static String getCollectionDifferentValue(List<String> list, Set<String> set, String flag) {
        String result = "";
        for (String str : list) {
            if (!set.contains(str)) {
                result += str + ",";
            }
        }
        if (StringUtils.isNotEmpty(result)) {
            result = StringUtils.strip(result, ",");
            result = "---" + flag + ":" + result + "为必填参数";
        }
        return result;
    }
}
